/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-09-27
 */
package org.jeecg.modules.flow.callback.outbound.order;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceiptMaterial;
import org.jeecg.modules.business.doc.outbound.entity.vo.OutBoundReceiptSaveVO;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryDocumentVO;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryVO;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.flow.callback.outbound.util.CommonOutboundUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 盘亏单出库单保存并提交回调bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-27
 */
@RequiredArgsConstructor
@Component
public class OutBoundReceiptPKDSubmitBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsOutReceiptService wmsOutReceiptService;
    private final IWmsRepertoryService wmsRepertoryService;
    private final CommonOutboundUtil commonOutboundUtil;

    /**
     * 业务逻辑 盘亏单出库单保存并提交
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {

        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        baseFlowDto.translateClass(OutBoundReceiptSaveVO.class);
        OutBoundReceiptSaveVO saveVO = (OutBoundReceiptSaveVO) baseFlowDto.getData();
        Boolean jumpSecondCheck = saveVO.getJumpSecondCheck();
        WmsOutReceipt wmsOutReceipt = new WmsOutReceipt();
        BeanUtils.copyProperties(saveVO, wmsOutReceipt);
        wmsOutReceipt.setId(null);
        String receiptId = wmsOutReceiptService.saveMain(wmsOutReceipt, saveVO.getWmsOutReceiptMaterialList(), false);
        //校验即时库存
        wmsRepertoryService.outSubmit(receiptId, true);
        //判断是否能分批出库提交
        commonOutboundUtil.checkCanExecuteBatchCommit(wmsOutReceipt.getWmsOutReceiptTaskId(),receiptId,jumpSecondCheck,baseFlowDto);
        //校验是否装车复核完成
        commonOutboundUtil.checkNeedLoadCheckCommit(wmsOutReceipt.getWmsOutReceiptTaskId(),receiptId);
        List<WmsOutReceiptMaterial> receiptMaterials = saveVO.getWmsOutReceiptMaterialList();
        List<RepertoryVO> collect = receiptMaterials.stream().map(o -> {
            RepertoryVO repertoryVO = BeanUtil.copyProperties(o, RepertoryVO.class);
            repertoryVO.setStockQuantity(o.getOutboundQuantity());
            repertoryVO.setStockQuantitySub(o.getOutboundQuantityAssist());
            return repertoryVO;
        }).collect(Collectors.toList());
        RepertoryDocumentVO repertoryDocumentVO = new RepertoryDocumentVO();
        repertoryDocumentVO.setInvoicesId(receiptId);
        repertoryDocumentVO.setDocumentType(2);
        repertoryDocumentVO.setRepertoryVOS(collect);
        //扣除即时库存
        wmsRepertoryService.reduce(repertoryDocumentVO);
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(receiptId);
    }

}
